package com.guodefu.unifiedresponse.entity;

import com.guodefu.unifiedresponse.IResult;
import com.guodefu.unifiedresponse.ResultEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serial;
import java.io.Serializable;

/**
 * API 统一响应对象
 *
 * @author guodefu909
 */

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Response<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    /**
     * 状态码
     */
    private String code;

    /**
     * 状态描述
     */
    private String message;

    /**
     * 应用名
     */
    private String appName;

    /**
     * 详细信息
     */
    private T data;

    private Response(IResult resultCode, String appName, T data) {
        this(resultCode.getCode(), resultCode.getMessage(), appName, data);
    }

    public static <T> Response<T> success(String appName, T data) {
        return new Response<>(ResultEnum.SUCCESS, appName, data);
    }

    public static <T> Response<T> fail(IResult resultCode, String appName, T data) {
        return new Response<>(resultCode.getCode(), resultCode.getMessage(), appName, data);
    }

    public static <T> Response<T> fail(String code, String message, String appName, T data) {
        return new Response<>(code, message, appName, data);
    }

    public boolean isSuccess() {
        return code.equals(ResultEnum.SUCCESS.getCode());
    }

}
